var app = getApp();

function view(id,callback) {
  console.log("calendar view ... id : " + id);
  wx.showLoading();
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.calendar.view',
      id: id,
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

function edit(id, title, content, type, owner, yuyue_session, callback){
  console.log("calendar edit ... id : " + id);
  wx.showLoading();
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.calendar.edit',
      id: id,
      title: title,
      content: content,
      type: type,
      owner: owner,
      yuyue_session: yuyue_session,
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

function my_calendar_list(callback) {
  console.log("my_calendar_list  ...  ");
  wx.showLoading();
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.calendar.my_calendar_list',
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

function subscribe(cid, callback) {
  wx.showLoading();
  console.log('now calendar subscribe... cid :' + cid);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.calendar.subscribe',
      calendar: cid,
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
     wx.hideLoading();
    }
  });
}


function unsubscribe(cid, callback) {
  wx.showLoading();
  console.log('now calendar unsubscribe... cid :' + cid);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.calendar.unsubscribe',
      calendar: cid,
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}

function check_subscribe(cid, callback) {
  wx.showLoading();
  console.log('now calendar check_subscribe... cid :' + cid);
  wx.request({
    url: app.globalData.default_url,
    data: {
      action: 'api.v1.calendar.check_subscribe',
      calendar: cid,
      yuyue_session: wx.getStorageSync("yuyue_session"),
    },
    success: function (res) {
      callback(res);
    },
    complete: function () {
      wx.hideLoading();
    }
  });
}
module.exports = {
  view: view,
  edit: edit,
  my_calendar_list: my_calendar_list,
  check_subscribe: check_subscribe,
  unsubscribe: unsubscribe,
  subscribe: subscribe,
}